package com.ctgu.elasticsearch.config.rabbitMQ;

import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.DirectExchange;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.support.converter.Jackson2JsonMessageConverter;
import org.springframework.amqp.support.converter.MessageConverter;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @ ClassName UserQueueConfig
 * @ Author Alex2
 * @ Date 2025/8/24 17:19
 **/
@Configuration
public class UserQueueConfig {
    public static final String USER_EXCHANGE = "user_exchange";

    public static final String INSERT_USER_QUEUE = "insert_user_queue";
    public static final String INSERT_USER_ROUTING_KEY = "insert_user_routing_key";

    public static final String UPDATE_USER_QUEUE = "update_user_queue";
    public static final String UPDATE_USER_ROUTING_KEY = "update_user_routing_key";

    public static final String DELETE_USER_QUEUE = "delete_user_queue";
    public static final String DELETE_USER_ROUTING_KEY = "delete_user_routing_key";

    // 创建交换机
    @Bean
    public DirectExchange userExchange() {
        return new DirectExchange(USER_EXCHANGE);
    }

    // 创建队列
    @Bean
    public Queue insertQueue() {
        return new Queue(INSERT_USER_QUEUE, true);
    }

    @Bean
    public Queue updateQueue() {
        return new Queue(UPDATE_USER_QUEUE, true);
    }

    @Bean
    public Queue deleteQueue() {
        return new Queue(DELETE_USER_QUEUE, true);
    }

    // 绑定交换机
    @Bean
    public Binding insertBinding(Queue insertQueue, DirectExchange userExchange) {
        return BindingBuilder.bind(insertQueue).to(userExchange).with(INSERT_USER_ROUTING_KEY);
    }

    @Bean
    public Binding updateBinding(Queue updateQueue, DirectExchange userExchange) {
        return BindingBuilder.bind(updateQueue).to(userExchange).with(UPDATE_USER_ROUTING_KEY);
    }

    @Bean
    public Binding deleteBinding(Queue deleteQueue, DirectExchange userExchange) {
        return BindingBuilder.bind(deleteQueue).to(userExchange).with(DELETE_USER_ROUTING_KEY);
    }

    // 消息转换器
    @Bean
    public MessageConverter messageConverter() {
        return new Jackson2JsonMessageConverter();
    }
}
